package com.secneo.appshare.activities;

import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;

import com.secneo.appshare.AppShareApplication;
import com.secneo.appshare.R;
import com.secneo.appshare.adapter.FeedsAdapter;
import com.secneo.appshare.adapter.FeedsReceivedAttentAdapter;
import com.secneo.appshare.adapter.FeedsRequestAttentAdapter;
import com.secneo.appshare.api.LocalStore;
import com.secneo.appshare.api.RemoteApi.Feed;
import com.secneo.appshare.api.RemoteApi.Feeds;
import com.secneo.appshare.api.impl.RemoteApiImpl;
import com.secneo.appshare.common.Constants;
import com.secneo.appshare.threads.FeedsThread;
import com.secneo.mp.api.database.DatabaseHelper;

public class MyZoneAttentActivity extends CommonMenuActivity implements OnClickListener
{
	
	
	enum MyZoneAttentPage{
		MyAttentPage ,
		RequestAttentPage ,
		ReceivedAttentPage
	}

	private int mPageNumber;
	private int mFocusPosition;
	private boolean mReadyToGetFeeds = false;
	private ProgressDialog mDynDisPMProgressBar = null;
	private Feeds mLocalFeeds = new Feeds();
	
	private int mRejectPosition;
	//if 0 means all selected, otherwise it means the single selected position
	
	private LinearLayout mZoneAttentLayout;
	private TextView mMyAttentTxt;
	private GridView mMyAttentGridView;

	private LinearLayout mZoneRequestAttentLayout;
	private TextView mRequestAttentTxt;
	private Button mBtnRequestAllAttent;
	private ListView mRequestAttentListView;

	private LinearLayout mZoneReceivedAttentLayout;
	private TextView mReceivedAttentTxt;
	private Button mBtnAllAttent;
	private ListView mReceivedAttentListView;
	
	private RadioButton buttonMyAttent;
	private RadioButton buttonRequestAttent; 
	private RadioButton buttonReceivedAttent;
	
	private Button mRetIv;
	
	private MyZoneAttentPage currentControl ;
	
	private LinearLayout progress_layout;
	private View errorView;
	
	private FeedsThread zoneAttentThread = null;
	
	private FeedsAdapter feedAdapter = null;
	private FeedsRequestAttentAdapter requestAttentAdapter = null;
	private FeedsReceivedAttentAdapter receivedAttentAdapter = null;
	
	
	public static final int REJECT_ALL_REQUEST = -1;
	
	public static final int MSG_GET_OTHER_USER_ICON = 101;
	Drawable dw =null;
    public int radioChecked=-1; 
    
    public Handler mHandler = new Handler()
	{
		@Override
		public void handleMessage(Message msg)
		{
			switch (msg.what)
			{
				case Constants.MSG_GET_FEEDS_NETWORK_ERROR:
					mReadyToGetFeeds = true;
					if(mDynDisPMProgressBar != null && mDynDisPMProgressBar.isShowing()){
						mDynDisPMProgressBar.dismiss() ;
					}
					showNetworkErrView(Constants.MSG_GET_FEEDS_NETWORK_ERROR) ;
					break;
					
				case Constants.MSG_GET_MY_ATTENT_FINISH:
					
					mReadyToGetFeeds = true;
					if(mDynDisPMProgressBar != null && mDynDisPMProgressBar.isShowing()){
						mDynDisPMProgressBar.dismiss() ;
					}
					
					updateFeeds(zoneAttentThread.getFeeds());
					
					progress_layout.setVisibility(View.GONE);
					refreshMyAttentGridView(mLocalFeeds);
					break;
					
				case Constants.MSG_GET_REQUEST_ATTENT_FINISH:
					
					mReadyToGetFeeds = true;
					if(mDynDisPMProgressBar != null && mDynDisPMProgressBar.isShowing()){
						mDynDisPMProgressBar.dismiss() ;
					}
					
					updateFeeds(zoneAttentThread.getFeeds());
					
					progress_layout.setVisibility(View.GONE);
					refreshRequestAttentListView(mLocalFeeds);
					break;
					
				case Constants.MSG_GET_RECEIVED_ATTENT_FINISH:

					mReadyToGetFeeds = true;
					if(mDynDisPMProgressBar != null && mDynDisPMProgressBar.isShowing()){
						mDynDisPMProgressBar.dismiss() ;
					}
					
					updateFeeds(zoneAttentThread.getFeeds());
					
					progress_layout.setVisibility(View.GONE);
					refreshReceivedAttentListView(mLocalFeeds);
					break;
					
				case Constants.MSG_GET_FEEDICON_FINISH:
					if(buttonMyAttent.isChecked())
					{
						mMyAttentGridView.invalidateViews();
						return;
					}
					else if(buttonRequestAttent.isChecked())
					{
						mRequestAttentListView.invalidateViews();
						return;
					}
					else if(buttonReceivedAttent.isChecked())
					{
						mReceivedAttentListView.invalidateViews();
						return;
					}
					break;
					
				default:
					break ;
			}
			super.handleMessage(msg);
		}

	};
	

	private Handler mAttentMultipleHandler = new Handler() {
		public void handleMessage(Message msg) {
			switch(msg.what) 
			{
			case Constants.MSG_ATTENT_MULTIPLE_FINISH:
				try{
					requestAttentAdapter.setFeedList(mLocalFeeds.feeds);
					requestAttentAdapter.notifyDataSetChanged();
				
				} catch(Exception ex) {
					ex.printStackTrace();
				}
				break;

			case Constants.MSG_ATTENT_SINGLE_FINISH:
				try{
					String finishedUid = requestAttentAdapter.getFinishedUid();

					int size = mLocalFeeds.feeds.size();
					for(int i=0; i<size; i++)
					{
						Feed feed = mLocalFeeds.feeds.get(i);
						if(feed.uid.equals(finishedUid))
						{
							mLocalFeeds.feeds.get(i).is_attent = 1;
							break;
						}
					}

					requestAttentAdapter.setFeedList(mLocalFeeds.feeds);
					requestAttentAdapter.notifyDataSetChanged();
					
				} catch(Exception ex) {
					ex.printStackTrace();
				}
				break;
				

			case Constants.MSG_ACCEPT_MULTIPLE_FINISH:
				try{
					receivedAttentAdapter.setFeedList(mLocalFeeds.feeds);
					receivedAttentAdapter.notifyDataSetChanged();
				
				} catch(Exception ex) {
					ex.printStackTrace();
				}
				break;

			case Constants.MSG_ACCEPT_SINGLE_FINISH:
				try{
					String finishedUid = receivedAttentAdapter.getFinishedUid();

					int size = mLocalFeeds.feeds.size();
					for(int i=0; i<size; i++)
					{
						Feed feed = mLocalFeeds.feeds.get(i);
						if(feed.uid.equals(finishedUid))
						{
							mLocalFeeds.feeds.get(i).is_attent = 1;
							break;
						}
					}
					
					receivedAttentAdapter.setFeedList(mLocalFeeds.feeds);
					receivedAttentAdapter.notifyDataSetChanged();
					
				} catch(Exception ex) {
					ex.printStackTrace();
				}
				break;

			case Constants.MSG_REJECT_MULTIPLE_FINISH:
				try{
					receivedAttentAdapter.setFeedList(mLocalFeeds.feeds);
					receivedAttentAdapter.notifyDataSetChanged();
				
				} catch(Exception ex) {
					ex.printStackTrace();
				}
				break;

			}
			
			super.handleMessage(msg);
		}
	};
	

	private void clearFeeds(){
		mPageNumber = 1;
		mFocusPosition = 0;
		
		if(null!=mLocalFeeds.feeds){
			mLocalFeeds.feeds.clear();
		}
	}


	private void updateFeeds(Feeds feeds){
		++mPageNumber;
		if(null==mLocalFeeds.feeds){
			mLocalFeeds.feeds = new ArrayList<Feed>();
		}
		mLocalFeeds.feeds.addAll(feeds.feeds);
		mLocalFeeds.fb_invite_popup = feeds.fb_invite_popup;
		mLocalFeeds.msgs = feeds.msgs;
		mLocalFeeds.total = feeds.total;
		mLocalFeeds.fansTotal = feeds.fansTotal;
		mLocalFeeds.attentTotal = feeds.attentTotal;
	}


	private void showNetworkErrView(int action){
		errorView.setVisibility(View.VISIBLE);
		progress_layout.setVisibility(View.GONE);
		enableToggleButtons(true);
	}
	
	public void refreshMyAttentGridView(final Feeds feeds) 
	{
		feedAdapter = new FeedsAdapter(this, feeds.feeds);
		mZoneAttentLayout.setVisibility(View.VISIBLE);

		int attentCount = feeds.total;
		int fansCount = feeds.fansTotal;
		
		String myAttentTitle = MyZoneAttentActivity.this.getResources().getString(R.string.title_my_attent,
				attentCount, fansCount);
		mMyAttentTxt.setText(myAttentTitle);
		mMyAttentGridView.setNumColumns(4);
		mMyAttentGridView.setAdapter(feedAdapter);
		mMyAttentGridView.setSelection(mFocusPosition);
		mMyAttentGridView.setOnItemClickListener(new OnItemClickListener()
		{
			public void onItemClick(AdapterView parent, View v, int position, long id)
			{
				try 
				{
					Feed userInfo = feeds.feeds.get(position);
					Intent intent = new Intent(); 
					intent.setClass(MyZoneAttentActivity.this, AppsOtherUserActivity.class);

					Bundle mBundle = new Bundle();
					String uid = LocalStore.getLocaleStoreInstance(MyZoneAttentActivity.this).getUid();
					mBundle.putString("uid", uid);
					mBundle.putString("other_uid", userInfo.uid);
					mBundle.putString("user_icon", userInfo.user_icon);
					mBundle.putString("user_name", userInfo.user_name);
					intent.putExtras(mBundle);
					startActivity(intent);
					
				} catch (Exception ex) {
					ex.printStackTrace();
				}
			}
		});
		
		enableToggleButtons(true);
	}
	

	public void refreshRequestAttentListView(final Feeds feeds) 
	{
		requestAttentAdapter = new FeedsRequestAttentAdapter(this, mAttentMultipleHandler, feeds.feeds);
		mZoneRequestAttentLayout.setVisibility(View.VISIBLE);

		int notFansCount = feeds.total;

		String requestAttentTitle = MyZoneAttentActivity.this.getResources().getString(R.string.title_request_attent, notFansCount);
		mRequestAttentTxt.setText(requestAttentTitle);
		
		mRequestAttentListView.setAdapter(requestAttentAdapter);
		mRequestAttentListView.setSelection(mFocusPosition);
		mRequestAttentListView.setOnItemClickListener(new OnItemClickListener()
		{
			public void onItemClick(AdapterView parent, View v, int position, long id)
			{
				try 
				{
					requestAttentAdapter.setCheckBoxOnOff(position);
					requestAttentAdapter.notifyDataSetChanged();
					
				} catch (Exception ex) {
					ex.printStackTrace();
				}
			}
		});
		
		enableToggleButtons(true);
	}
	

	public void refreshReceivedAttentListView(final Feeds feeds) 
	{
		receivedAttentAdapter = new FeedsReceivedAttentAdapter(this, mAttentMultipleHandler, feeds.feeds);
		mZoneReceivedAttentLayout.setVisibility(View.VISIBLE);

		int attentRequestCount = feeds.feeds.size();

		String receiveAttentTitle = MyZoneAttentActivity.this.getResources().getString(R.string.title_received_attent, attentRequestCount);
		mReceivedAttentTxt.setText(receiveAttentTitle);
		
		mReceivedAttentListView.setAdapter(receivedAttentAdapter);
		mReceivedAttentListView.setSelection(mFocusPosition);
		mReceivedAttentListView.setOnItemLongClickListener(new OnItemLongClickListener()
		{

			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				Log.e("MyTag", "-----onItemLongClick-----");
				try 
				{
					mRejectPosition = arg2;
					new AlertDialog.Builder(MyZoneAttentActivity.this).setIcon(
							android.R.drawable.ic_dialog_alert).setTitle("关注请求处理")
							.setSingleChoiceItems(new String[] { 
									"删除", "清空记录" },
									radioChecked, new DialogInterface.OnClickListener() {

										public void onClick(DialogInterface dialog, int which)
										{
											radioChecked = which;
											Log.d("which", "which="+which+"radioChecked"+radioChecked);
											if(which==0)
											{
												//mRejectPosition has already been set at the beginning
												startRejectAttentThread();
											}
											if(which==1)
											{
												mRejectPosition = REJECT_ALL_REQUEST;
												startRejectAttentThread();
											}
											dialog.dismiss();
										}

									}).create().show();
					
				} catch (Exception ex) {
					ex.printStackTrace();
				}
				return true;
			}
		});
		mReceivedAttentListView.setOnItemClickListener(new OnItemClickListener()
		{
			public void onItemClick(AdapterView parent, View v, int position, long id)
			{
				try 
				{
					receivedAttentAdapter.setCheckBoxOnOff(position);
					receivedAttentAdapter.notifyDataSetChanged();
					
				} catch (Exception ex) {
					ex.printStackTrace();
				}
			}
		});
		
		enableToggleButtons(true);
	}
	

	
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.appshare_zone_attent);
		AppShareApplication.getInstance().addActivity(this);
		
		mPageNumber = 1;

		mZoneAttentLayout = (LinearLayout) findViewById(R.id.zone_attent_layout);
		mMyAttentTxt = (TextView) findViewById(R.id.my_attent_txt);
		mMyAttentGridView = (GridView) findViewById(R.id.gridview_attent_feeds);
		mMyAttentGridView.setOnScrollListener(mGridViewScrollListner) ;

		mZoneRequestAttentLayout = (LinearLayout) findViewById(R.id.zone_request_attent_layout);
		mRequestAttentTxt = (TextView) findViewById(R.id.request_attent_txt);
		mBtnRequestAllAttent = (Button) findViewById(R.id.btnRequestAllAttent);
		mRequestAttentListView = (ListView) findViewById(R.id.listview_request_attent);
		mRequestAttentListView.setOnScrollListener(mRequestListViewScrollListner) ;

		mZoneReceivedAttentLayout = (LinearLayout) findViewById(R.id.zone_received_attent_layout);
		mReceivedAttentTxt = (TextView) findViewById(R.id.received_attent_txt);
		mBtnAllAttent = (Button) findViewById(R.id.btnAllAttent);
		mReceivedAttentListView = (ListView) findViewById(R.id.listview_received_attent);
		mReceivedAttentListView.setOnScrollListener(mReceivedListViewScrollListner) ;

		mRequestAttentListView.setDivider(getResources().getDrawable(R.drawable.list_divider));
		mReceivedAttentListView.setDivider(getResources().getDrawable(R.drawable.list_divider));

		mBtnRequestAllAttent.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				DatabaseHelper dh=new DatabaseHelper(MyZoneAttentActivity.this);
				dh.insertUserAppPopularityBycatagory(MyZoneAttentActivity.this,2034);
				dh.close();
				
				startRequestAttentThread();
			}
		});
		
		mBtnAllAttent.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				startAcceptAttentThread();
			}
		});
		
		
		progress_layout = (LinearLayout) findViewById(R.id.progress_layout);
		errorView = MyZoneAttentActivity.this.findViewById(R.id.error_user);
		
		buttonMyAttent = (RadioButton) findViewById(R.id.button_my_attent);
		buttonMyAttent.setOnClickListener(this);
		buttonMyAttent.setChecked(true);
		buttonRequestAttent = (RadioButton) findViewById(R.id.button_request_attent);
		buttonRequestAttent.setOnClickListener(this);
		buttonReceivedAttent = (RadioButton) findViewById(R.id.button_received_attent);
		buttonReceivedAttent.setOnClickListener(this);
		
		mRetIv = (Button)findViewById(R.id.img_ret) ;
		mRetIv.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				finish();
			}
		}) ;
		
		currentControl = MyZoneAttentPage.MyAttentPage ;
		
	}

	
	
	
	@Override
	public void onClick(View v)
	{
		if (v instanceof RadioButton) 
		{			
			mMyAttentGridView.setAdapter(null);
			if(feedAdapter != null)
			{
				feedAdapter.freeDrawable();
				feedAdapter=null;
			}
			mRequestAttentListView.setAdapter(null);
			if(requestAttentAdapter != null)
			{
				requestAttentAdapter.freeDrawable();
				requestAttentAdapter=null;
			}
			mReceivedAttentListView.setAdapter(null);
			if(receivedAttentAdapter != null)
			{
				receivedAttentAdapter.freeDrawable();
				receivedAttentAdapter=null;
			}
			
			errorView.setVisibility(View.GONE);
			enableToggleButtons(false);
			if(buttonMyAttent.isChecked())
			{
				mReadyToGetFeeds = false;
				clearFeeds();
				startGetMyAttent();
				return;
			}
			else if(buttonRequestAttent.isChecked())
			{
				DatabaseHelper dh=new DatabaseHelper(MyZoneAttentActivity.this);
				dh.insertUserAppPopularityBycatagory(MyZoneAttentActivity.this,2055);
				dh.close();

				mReadyToGetFeeds = false;
				clearFeeds();
				startGetRequestAttent();
				return;
			}
			else if(buttonReceivedAttent.isChecked())
			{
				DatabaseHelper dh=new DatabaseHelper(MyZoneAttentActivity.this);
				dh.insertUserAppPopularityBycatagory(MyZoneAttentActivity.this,2035);
				dh.close();

				mReadyToGetFeeds = false;
				clearFeeds();
				startGetReceivedAttent();
				return;
			}
		}
	}
	
	// 获得我的关注列表
	private void startGetMyAttent()
	{
		hideCurrentPage();
		
		currentControl = MyZoneAttentPage.MyAttentPage;
		progress_layout.setVisibility(View.VISIBLE);
		enableToggleButtons(false);
		
		String uid = LocalStore.getLocaleStoreInstance(this).getUid();
		zoneAttentThread = new FeedsThread(this, mHandler, mPageNumber, uid, Constants.USER_TYPE_MY_ATTENT, Constants.feedGetTypeAll);
		zoneAttentThread.start();
	}
	
	// 获得求关注人列表
	private void startGetRequestAttent()
	{
		hideCurrentPage();
		
		currentControl = MyZoneAttentPage.RequestAttentPage;
		progress_layout.setVisibility(View.VISIBLE);
		enableToggleButtons(false);
		
		String uid = LocalStore.getLocaleStoreInstance(this).getUid();
		zoneAttentThread = new FeedsThread(this, mHandler, mPageNumber, uid, Constants.USER_TYPE_REQUEST_ATTENT, Constants.feedGetTypeAll);
		zoneAttentThread.start();
	}


	// 获得收到请求关注列表
	private void startGetReceivedAttent()
	{
		hideCurrentPage();
		
		currentControl = MyZoneAttentPage.ReceivedAttentPage;
		progress_layout.setVisibility(View.VISIBLE);
		enableToggleButtons(false);
		
		String uid = LocalStore.getLocaleStoreInstance(this).getUid();
		zoneAttentThread = new FeedsThread(this, mHandler, mPageNumber, uid, Constants.USER_TYPE_RECEIVED_ATTENT, Constants.feedGetTypeAll);
		zoneAttentThread.start();
	}
	
	
	private void hideCurrentPage(){
		progress_layout.setVisibility(View.GONE);
		mZoneAttentLayout.setVisibility(View.GONE);
		mZoneRequestAttentLayout.setVisibility(View.GONE);
		mZoneReceivedAttentLayout.setVisibility(View.GONE);
		errorView.setVisibility(View.GONE);
		/*
		switch(currentControl){
			case MyAttentPage:
				progress_layout.setVisibility(View.GONE);
				mZoneAttentLayout.setVisibility(View.GONE);
				mZoneRequestAttentLayout.setVisibility(View.GONE);
				errorView.setVisibility(View.GONE);
				break;
			case RequestAttentPage:
				progress_layout.setVisibility(View.GONE);
				mZoneRequestAttentLayout.setVisibility(View.GONE) ;
				errorView.setVisibility(View.GONE);
				break;
			case ReceivedAttentPage:
				progress_layout.setVisibility(View.GONE);
				mZoneAttentLayout.setVisibility(View.GONE) ;
				errorView.setVisibility(View.GONE);
				break;
		}
		*/
	}

	
	private void enableToggleButtons(boolean enable)
	{
		buttonMyAttent.setClickable(enable);
		buttonRequestAttent.setClickable(enable);
		buttonReceivedAttent.setClickable(enable);
	}
	

	@Override
	protected void onStart() 
	{
		if(currentControl == MyZoneAttentPage.MyAttentPage)
		{
			startGetMyAttent();
		}
		super.onRestart();
	}

	
	
	@Override
	protected void onDestroy() 
	{
		zoneAttentThread.stopRun();
		mMyAttentGridView.setAdapter(null);
		if(feedAdapter != null)
		{
			feedAdapter.freeDrawable();
			feedAdapter=null;
		}
		mRequestAttentListView.setAdapter(null);
		if(requestAttentAdapter != null)
		{
			requestAttentAdapter.freeDrawable();
			requestAttentAdapter=null;
		}
		mReceivedAttentListView.setAdapter(null);
		if(receivedAttentAdapter != null)
		{
			receivedAttentAdapter.freeDrawable();
			receivedAttentAdapter=null;
		}

		if(mDynDisPMProgressBar != null && mDynDisPMProgressBar.isShowing()){
			mDynDisPMProgressBar.dismiss() ;
		}
		
		clearFeeds();
		
		super.onDestroy();
	}
	

	private void startRequestAttentThread()
	{
		Thread followMultipleThread = new Thread() 
		{
			public void run()
			{
				final String uid = LocalStore.getLocaleStoreInstance().getUid();
				int size = mLocalFeeds.feeds.size();
				List<String> uidList = new ArrayList<String>();
				for(int i=0; i<size; i++){
					if(mLocalFeeds.feeds.get(i).box_checked){
						String name = mLocalFeeds.feeds.get(i).user_name;
						Log.e("MyTag", "Selected:"+name);
						uidList.add(mLocalFeeds.feeds.get(i).uid);
					}
				}
				RemoteApiImpl remoteApi = new RemoteApiImpl();
				boolean ret = remoteApi.uploadRequestAttent(MyZoneAttentActivity.this, uid, uidList);
				if(ret)
				{
					size = mLocalFeeds.feeds.size();
					int uidListSize=0;
					for(int i=0; i<size; i++)
					{
						uidListSize = uidList.size();
						Feed feed = mLocalFeeds.feeds.get(i);
						for(int j=0; j<uidListSize; j++)
						{
							if(feed.uid.equals(uidList.get(j)))
							{
								mLocalFeeds.feeds.get(i).is_attent = 1;
								uidList.remove(j);
								break;
							}
						}
					}
					Message msg = new Message();
					msg.what = Constants.MSG_ATTENT_MULTIPLE_FINISH;
					mAttentMultipleHandler.sendMessage(msg);
				}
				
			}
		};
		followMultipleThread.start();
	}
	

	private void startAcceptAttentThread()
	{
		Thread followMultipleThread = new Thread() 
		{
			public void run()
			{
				final String uid = LocalStore.getLocaleStoreInstance().getUid();
				int size = mLocalFeeds.feeds.size();
				List<String> uidList = new ArrayList<String>();
				for(int i=0; i<size; i++){
					if(mLocalFeeds.feeds.get(i).box_checked){
						String name = mLocalFeeds.feeds.get(i).user_name;
						Log.e("MyTag", "Selected:"+name);
						uidList.add(mLocalFeeds.feeds.get(i).uid);
					}
				}
				RemoteApiImpl remoteApi = new RemoteApiImpl();
				boolean ret = remoteApi.uploadFollowedFriends(MyZoneAttentActivity.this, uid, uidList);
				if(ret)
				{
					size = mLocalFeeds.feeds.size();
					int uidListSize=0;
					for(int i=0; i<size; i++)
					{
						uidListSize = uidList.size();
						Feed feed = mLocalFeeds.feeds.get(i);
						for(int j=0; j<uidListSize; j++)
						{
							if(feed.uid.equals(uidList.get(j)))
							{
								mLocalFeeds.feeds.get(i).is_attent = 1;
								uidList.remove(j);
								break;
							}
						}
					}
					Message msg = new Message();
					msg.what = Constants.MSG_ACCEPT_MULTIPLE_FINISH;
					mAttentMultipleHandler.sendMessage(msg);
				}
				
			}
		};
		followMultipleThread.start();
	}
	

	private void startRejectAttentThread()
	{
		Thread followMultipleThread = new Thread() 
		{
			public void run()
			{
				int rejectPos = mRejectPosition;
				
				final String uid = LocalStore.getLocaleStoreInstance().getUid();
				int size = mLocalFeeds.feeds.size();
				List<String> uidList = new ArrayList<String>();
				
				if(REJECT_ALL_REQUEST==rejectPos){
					for(int i=0; i<size; i++){
						if(mLocalFeeds.feeds.get(i).box_checked){
							String name = mLocalFeeds.feeds.get(i).user_name;
							Log.e("MyTag", "Selected:"+name);
							uidList.add(mLocalFeeds.feeds.get(i).uid);
						}
					}
				}
				else{
					uidList.add(mLocalFeeds.feeds.get(rejectPos).uid);
				}
				
				RemoteApiImpl remoteApi = new RemoteApiImpl();
				boolean ret = remoteApi.uploadRejectAttent(MyZoneAttentActivity.this, uid, uidList);
				if(ret)
				{
					size = mLocalFeeds.feeds.size();
					int uidListSize=0;
					for(int i=0; i<size; i++)
					{
						uidListSize = uidList.size();
						Feed feed = mLocalFeeds.feeds.get(i);
						for(int j=0; j<uidListSize; j++)
						{
							if(feed.uid.equals(uidList.get(j)))
							{
								mLocalFeeds.feeds.remove(i);
								size--;
								i--;
								uidList.remove(j);
								break;
							}
						}
					}
					Message msg = new Message();
					msg.what = Constants.MSG_REJECT_MULTIPLE_FINISH;
					mAttentMultipleHandler.sendMessage(msg);
				}
				
			}
		};
		followMultipleThread.start();
	}
	

	private OnScrollListener mGridViewScrollListner = new OnScrollListener()
	{
		private int firstItem = 0;
		private int lastItem = 0;
		@Override
		public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount)
		{
			firstItem = firstVisibleItem;
			lastItem = firstVisibleItem + visibleItemCount;
		}

		@Override
		public void onScrollStateChanged(AbsListView view, int scrollState)
		{
			if (feedAdapter != null && lastItem == feedAdapter.getCount() && scrollState == OnScrollListener.SCROLL_STATE_IDLE)
			{
				Log.e("MyTag", "Total:"+mLocalFeeds.total);
				Log.e("MyTag", "return Size:"+mLocalFeeds.feeds.size());
				
				if(mLocalFeeds.total==mLocalFeeds.feeds.size()){
					return;
				}
				if (!mReadyToGetFeeds)
					return;
				mReadyToGetFeeds = false;

            	mDynDisPMProgressBar = ProgressDialog.show(
            			MyZoneAttentActivity.this, null, "加载中，请稍候..." ,true);
				
				mFocusPosition = firstItem;
				
				String uid = LocalStore.getLocaleStoreInstance().getUid();
				zoneAttentThread = new FeedsThread(MyZoneAttentActivity.this, mHandler, mPageNumber, uid, 
						Constants.USER_TYPE_MY_ATTENT, Constants.feedGetTypeAll);	
				zoneAttentThread.start();
			}
		}
	};
	

	private OnScrollListener mRequestListViewScrollListner = new OnScrollListener()
	{
		private int firstItem = 0;
		private int lastItem = 0;
		@Override
		public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount)
		{
			firstItem = firstVisibleItem;
			lastItem = firstVisibleItem + visibleItemCount;
		}

		@Override
		public void onScrollStateChanged(AbsListView view, int scrollState)
		{
			if (requestAttentAdapter != null 
					&& lastItem == requestAttentAdapter.getCount() 
					&& scrollState == OnScrollListener.SCROLL_STATE_IDLE)
			{
				Log.e("MyTag", "Total:"+mLocalFeeds.total);
				Log.e("MyTag", "return Size:"+mLocalFeeds.feeds.size());
				
				if(mLocalFeeds.total==mLocalFeeds.feeds.size()){
					return;
				}
				if (!mReadyToGetFeeds)
					return;
				mReadyToGetFeeds = false;

            	mDynDisPMProgressBar = ProgressDialog.show(
            			MyZoneAttentActivity.this, null, "加载中，请稍候..." ,true);
				
				mFocusPosition = firstItem;
				
				String uid = LocalStore.getLocaleStoreInstance().getUid();
				
				zoneAttentThread = new FeedsThread(MyZoneAttentActivity.this, mHandler, mPageNumber, uid, 
						Constants.USER_TYPE_REQUEST_ATTENT, Constants.feedGetTypeAll);					
				zoneAttentThread.start();
			}
		}
	};
	

	private OnScrollListener mReceivedListViewScrollListner = new OnScrollListener()
	{
		private int firstItem = 0;
		private int lastItem = 0;
		@Override
		public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount)
		{
			firstItem = firstVisibleItem;
			lastItem = firstVisibleItem + visibleItemCount;
		}

		@Override
		public void onScrollStateChanged(AbsListView view, int scrollState)
		{
			if (receivedAttentAdapter != null 
					&& lastItem == receivedAttentAdapter.getCount() 
					&& scrollState == OnScrollListener.SCROLL_STATE_IDLE)
			{
				Log.e("MyTag", "Total:"+mLocalFeeds.total);
				Log.e("MyTag", "return Size:"+mLocalFeeds.feeds.size());
				
				if(mLocalFeeds.total==mLocalFeeds.feeds.size()){
					return;
				}
				if (!mReadyToGetFeeds)
					return;
				mReadyToGetFeeds = false;

            	mDynDisPMProgressBar = ProgressDialog.show(
            			MyZoneAttentActivity.this, null, "加载中，请稍候..." ,true);
				
				mFocusPosition = firstItem;
				
				String uid = LocalStore.getLocaleStoreInstance().getUid();
				zoneAttentThread = new FeedsThread(MyZoneAttentActivity.this, mHandler, mPageNumber, uid, 
						Constants.USER_TYPE_RECEIVED_ATTENT, Constants.feedGetTypeAll);	
				zoneAttentThread.start();
			}
		}
	};
	
	
	
}
